package com.idearworld.controller;

import static com.idearworld.utils.ControllerUtil.failedResult;
import static com.idearworld.utils.ControllerUtil.successResult;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idearworld.beans.GlobalConstants;
import com.idearworld.beans.RegisterBean;
import com.idearworld.model.UserInfo;
import com.idearworld.service.UserInfoService;

@Controller
@RequestMapping("/user")
public class LoginManagerCtrl{

	@Autowired
	private UserInfoService userInfoService;
	
	@RequestMapping(value="/LogoutAction",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> logoutAction(HttpSession session
			){
		Object obj=session.getAttribute(GlobalConstants.LOGIN_USER);
		if(obj !=null){
			session.removeAttribute(GlobalConstants.LOGIN_USER);
		}
		return successResult("success");
	}
	
	@RequestMapping(value="/CheckLogin",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> checkLogin(HttpSession session){
		UserInfo user=null;
		Object obj=session.getAttribute(GlobalConstants.LOGIN_USER);
		if(obj !=null){
			user=(UserInfo)obj;
		}
		return successResult(user);
	}
	
	@RequestMapping(value="/LoginAction",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> loginAction(@RequestBody UserInfo loginUser,HttpSession session
			){
		UserInfo userInfo = this.userInfoService.getUser(loginUser.getUserName(), loginUser.getPassword());
		if(userInfo !=null){
			session.setAttribute(GlobalConstants.LOGIN_USER, userInfo);
		}else{
			return failedResult(GlobalConstants.ERROR_TYPE_NEED_LOGIN, "用户名或密码错误");
		}
		
		return successResult(userInfo);
	}
	@RequestMapping(value="/RegisterCheckUserName",method=RequestMethod.POST)
	@ResponseBody
	public boolean registCheckUserName(@RequestParam("userName") String userName){
		return this.userInfoService.registerCheckUserName(userName);
	}
	@RequestMapping(value="/RegisterCheckUserEmail",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> registCheckUserEmail(@RequestParam("userAccount") String userAccount){
		 return successResult(this.userInfoService.registerCheckUserEmail(userAccount));
	}
	@RequestMapping(value="/RegisterUserAction",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> registUser(@RequestBody RegisterBean register,
			HttpSession session
			){
		String sessionCaptcha=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(!sessionCaptcha.equals(register.getCaptcha())){
			return failedResult(GlobalConstants.ERROR_TYPE_ERROR_PARAM, "验证码错误！");
		}
		UserInfo newUser=new UserInfo();
		newUser.setUserAccount(register.getUserAccount());
		newUser.setUserName(register.getUserName());
		newUser.setPassword(register.getPassword());
		newUser.setCreateDate(new Date());
		int res=this.userInfoService.registerUser(newUser);
		register.setUserId(newUser.getUserId());
		return successResult(register);
	}
	
}
